package cn.taobaoke.restful.domain;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

/**
 * @author yangzuo
 * @since 2017-04-06
 */
public interface PIDRepository extends JpaRepository<PIDEntity, Long> {

    Optional<PIDEntity> findByPidAndDomainId(String pid, Long domainId);

    List<PIDEntity> findByAgentIdAndDomainId(Long agentId, Long domainId);

    Optional<PIDEntity> findFirstByIsAssignedAndDomainId(Boolean isAssigned, Long domainId);

    // TODO test this method
    Optional<PIDEntity> findTopByDomainIdAndPidEndsWith(Long domainId, String postfix);

    @Query("from PIDEntity entity where entity.isAssigned = true and entity.agentId = null and entity.domainId = :domainId")
    List<PIDEntity> findAdminPid(@Param("domainId") Long domainId);

}
